package cn.lg.soar.system.biz.modules.auth.model;

import cn.lg.soar.common.util.trans.annotations.Translate;
import cn.lg.soar.database.translate.TranslateByEntity;
import cn.lg.soar.system.biz.handler.UserStatusTranslateHandler;
import cn.lg.soar.system.biz.modules.auth.entity.User;
import cn.lg.soar.system.biz.modules.tenant.entity.Tenant;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class UserVo implements Serializable {

    private static final long serialVersionUID = 6850908496978491051L;

    @Schema(description = "主键id")
    private Long id;

    @Schema(description = "用户名（账号）", example = "test-user")
    private String username;

    @Schema(description = "昵称", example = "测试用户")
    private String nickname;

    @Schema(description = "头像")
    private String avatar;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "最后登录时间")
    private LocalDateTime lastLoginDate;

    @Schema(description = "当前所属租户id")
    @TranslateByEntity(Tenant.class)
    private Integer currentTenantId;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "用户类型（根据系统需要配置）")
    private Integer userType;

    @Translate(UserStatusTranslateHandler.class)
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "状态（根据系统需要配置）")
    private Integer status;

    public UserVo() {
    }

    public UserVo(User source) {
        this.id = source.getId();
        this.username = source.getUsername();
        this.nickname = source.getNickname();
        this.avatar = source.getAvatar();
        this.lastLoginDate = source.getLastLoginDate();
        this.currentTenantId = source.getCurrentTenantId();
        this.userType = source.getUserType();
        this.status = source.getStatus();
    }

    public User toUser(){
        User target = new User();
        target.setId(this.id);
        target.setUsername(this.username);
        target.setNickname(this.nickname);
        target.setAvatar(this.avatar);
        target.setLastLoginDate(this.lastLoginDate);
        target.setCurrentTenantId(this.currentTenantId);
        target.setUserType(this.userType);
        target.setStatus(this.status);
        return target;
    }
}
